Skip to content

Add diff() and round() methods for Index and a test for each #52551

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 37 commits into from
Apr 17, 2023

Conversation

GrammatikakisDimitris
Copy link
Contributor

@GrammatikakisDimitris GrammatikakisDimitris commented Apr 9, 2023

If periods is greater than 1, computes the difference between values that
are `periods` number of positions apart.

Parameters:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks the formatting here is off

# GH#19708
idx = Index([10, 20, 30, 40, 50])
diffs = idx.diff()
diffs_period2 = idx.diff(periods=2)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you use pytest.mark.parameterize to test different periods?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would you like pytest.mark.parametrize for the round_test as well?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes please

diffs = idx.diff()
diffs_period2 = idx.diff(periods=2)

assert diffs.equals(Index([np.nan, 10, 10, 10, 10]))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you make an expected = variable and then call tm.assert_index_equal(result, expected)?

@mroeschke mroeschke added Enhancement Index Related to the Index class or subclasses labels Apr 10, 2023
@pytest.mark.parametrize(
"periods, expected",
[
(1, Index([np.nan, 10, 10, 10, 10])),
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you move the Index call to the body of the test (same as below)

@GrammatikakisDimitris
Copy link
Contributor Author

Forgot to pass parameters periods and decimals in the call of the functions, i will fix this.

A new Index object with the computed differences.

"""
return Index(self.to_series().diff(periods))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
return Index(self.to_series().diff(periods))
return self._constructor(self.to_series().diff(periods))

A new Index with the rounded values.

"""
return Index(self.to_series().round(decimals))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
return Index(self.to_series().round(decimals))
return self._constructor(self.to_series().round(decimals))

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

Comment on lines 6781 to 6782
>>> diff_idx = idx.diff()
>>> print(diff_idx)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
>>> diff_idx = idx.diff()
>>> print(diff_idx)
>>> idx.diff()

>>> print(diff_idx)
Index([nan, 10.0, 10.0, 10.0, 10.0], dtype='float64')

This example creates a new `Index` object `idx` with values
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The rest of this explanation is not needed

Comment on lines 6814 to 6815
Example
-------
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
Example
-------
Examples
--------

Comment on lines 6818 to 6819
>>> rounded_idx = idx.round(decimals=2)
>>> print(rounded_idx)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
>>> rounded_idx = idx.round(decimals=2)
>>> print(rounded_idx)
>>> idx.round(decimals=2)

Comment on lines 6777 to 6778
Example
-------
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
Example
-------
Examples
--------

Copy link
Member

@mroeschke mroeschke left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good. Could you add a whatsnew entry in v2.1.0.rst?

@GrammatikakisDimitris
Copy link
Contributor Author

What should i do about the conflict?

@mroeschke
Copy link
Member

The conflict should be resolved without removing someone else's contribution

@GrammatikakisDimitris
Copy link
Contributor Author

Sorry for the mess, i pulled all the changes to fix the conflict. If you like i can open a new PR.

@mroeschke
Copy link
Member

I think all you need to do is

git fetch upstream
git merge upstream/main

into this branch

@mroeschke mroeschke added this to the 2.1 milestone Apr 17, 2023
@mroeschke mroeschke merged commit 1567d9e into pandas-dev:main Apr 17, 2023
@mroeschke
Copy link
Member

Thanks @GrammatikakisDimitris

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Enhancement Index Related to the Index class or subclasses
Projects
None yet
Development

Successfully merging this pull request may close these issues.

ENH: Index should have diff() method
2 participants